<template>
  <div ref="chartDiv" :style="currStyle"></div>
</template>

<script setup>
import {ref, watch, onMounted, nextTick, markRaw} from 'vue';
import * as echarts from "echarts";
import visual from "@/visual/js/visual";
import * as chart from "@/visual/js/echarts";
import JSON5 from 'json5';
import { parseColors } from "@/utils/color";

const props = defineProps({
  node: Object,
  compStyle: Object,
  compData: Object
});
const currNode = ref(props.node);
const chartDiv = ref();
const currChart = ref(null);
const currStyle = ref();

const init = async() => {
  if (currNode.value.compData.dataSourceType === '') {
    currNode.value.compData.dataSourceType = '静态数据';
  }
  const compStyle = props.compStyle || {};
  let style = '';
  style += visual.parseTpl('width: {value};', compStyle['width']);
  style += visual.parseTpl('height: {value};', compStyle['height']);
  currStyle.value = style;

  let defaultOption = {};
  try {
    defaultOption = JSON5.parse(compStyle['jsonOption'] || '{}');
  } catch (e) {
    console.log("jsonOption解释出错", e)
  }
  const dataList = (await visual.loadData(props.compData, {}, {}, currNode.value)).data || [];
  currNode.value.compData.dataList = JSON.stringify(dataList);

  const seriesData = [];
  dataList.forEach(data => {
    seriesData.push({
      name: data['标签'],
      value: data['系列'],
    });
  });

  const option = {
    animation: compStyle['animation'] || false,
    color: parseColors(compStyle['colorScheme']),
    title: chart.parseTitle(compStyle),
    tooltip: chart.parseTooltip(compStyle),
    legend: chart.parseLegend(compStyle),
    series: [
      {
        ...parseFunnel(compStyle),
        data: seriesData
      }
    ],
    toolbox: chart.parseToolbox(compStyle)
  };

  await nextTick(() => {
    try {
      if (currChart.value) {
        echarts.dispose(chartDiv.value);
      }
      currChart.value = markRaw(echarts.init(chartDiv.value));
      currChart.value.setOption(Object.assign({}, option, defaultOption));
    } catch (e) {
      console.log(e);
    }
  });
  await nextTick(() => currChart.value.resize());
};

const parseFunnel = (compStyle) => {
  const result = { type: 'funnel' };

  result['min'] = compStyle['series.min'] || 0;
  result['max'] = compStyle['series.max'] || 100;
  result['minSize'] = compStyle['series.minSize'] || '0%';
  result['maxSize'] = compStyle['series.maxSize'] || '100%';
  result['sort'] = compStyle['series.sort'] || 'descending';
  result['orient'] = compStyle['series.orient'] || 'vertical';
  result['funnelAlign'] = compStyle['series.funnelAlign'] || 'center';
  result['gap'] = compStyle['series.gap'] || 0;
  result['label'] = {
    show: !!compStyle['series.label'],
    position: compStyle['series.position'] || 'inside',
  }
  const margin = (compStyle['margin'] || '10,0,30,0').split(',');
  if (margin.length >= 4) {
    result['top'] = parseInt(margin[0]);
    result['right'] = parseInt(margin[1]);
    result['bottom'] = parseInt(margin[2]);
    result['left'] = parseInt(margin[3]);
  }
  return result;
};

watch(() => props.compStyle, () => {
  init();
}, { deep: true });

watch(() => props.compData?.refresh, () => {
  init();
});

onMounted(() => {
  init();
});
</script>